import React from 'react';
import { observable, action, toJS } from 'mobx';
import {inject, observer} from 'mobx-react';
import {Row, Col, Input, Tabs, Button, Alert, Spin, Icon, Form, InputNumber, Modal, message} from 'antd'
import { WeaForm, WeaSwitch,WeaTableNew } from 'comsMobx';
import {
    WeaLocaleProvider,
    WeaErrorPage,
    WeaAlertPage,
    WeaTop,
    WeaTab,
    WeaInput,
    WeaLeftRightLayout,
    WeaSearchGroup,
    WeaRightMenu,
    WeaInputSearch,
    WeaBrowser,
    WeaSelect,
    WeaDialog,
    WeaFormItem,
    WeaProgress,
    WeaTools
} from 'ecCom'

const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;
const WeaTable = WeaTableNew.WeaTable;
const ButtonGroup = Button.Group;

@inject('DispatchReportStore')
@observer
class DispatchReport extends React.Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {//一些初始化请求
        const { DispatchReportStore } = this.props;
        //初始化列表
        DispatchReportStore.doSearch();
        DispatchReportStore.getRightMenu();
        DispatchReportStore.getConditionInfo();
    }
    componentWillReceiveProps(nextProps) {
    }

    componentWillUnmount() {
        //组件卸载时一般清理一些状态
    }

    render() {
        const { DispatchReportStore } = this.props;
        const { sessionkey, setShowSearchAd,loading , form,tableStore ,showSearchAd ,setQuickSearchValue } = DispatchReportStore;
        return (
            <div>
                <WeaRightMenu datas={this.getRightMenu()}>
                    <WeaTop
                        title={getLabel(128469, "派工")+getLabel(352, "统计")}
                        loading={loading}
                        icon={<i className='icon-coms-workflow'/>}
                        iconBgcolor={"#0079DE"}
                        buttons={this.getButtons()}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                    >
                        <WeaTab
                            buttonsAd={this.getTabButtonsAd()}
                            searchType={['base','advanced']}
                            searchsBaseValue={form.getFormParams()[this.quickSearchPara] || ''}
                            searchsAd={this.getSearchs()}
                            showSearchAd={showSearchAd}
                            setShowSearchAd={() => {setShowSearchAd(!showSearchAd);}}
                            hideSearchAd={()=>DispatchReportStore.setShowSearchAd(false)}
                            onSearch={v=>DispatchReportStore.doSearch({...form.getFormParams()})}
                            onSearchChange={value => {
                                form.updateFields({ [this.quickSearchPara]: { value } })
                                setQuickSearchValue(value);
                            }}
                        />
                        <WeaTable
                            loading={loading}
                            sessionkey={sessionkey}
                            comsWeaTableStore={tableStore}
                            hasOrder={true}
                            needScroll={true}
                        />
                    </WeaTop>
                </WeaRightMenu>
            </div>
        )
    }

    //页面右键
    getRightMenu() {
        const { DispatchReportStore } = this.props;
        const { rightMenu,tableStore,form } = DispatchReportStore;
        const count = tableStore.count;
        let isDisabled = count === 0 ;
        let btnArr = [];
        rightMenu.slice().forEach(m => {
            let onClick = () => {
            };
            if (m.menuFun.indexOf('doExcel()') >= 0) {
                onClick = () => {
                    DispatchReportStore.exportExcel({...form.getFormParams()});
                }
            }

            btnArr.push({
                key: m.type,
                icon: <i className={m.menuIcon}/>,
                content: m.menuName,
                disabled:m.isControl == '1' && isDisabled ,
                onClick,
            });
        });
        return btnArr;
    }

    //高级搜索查询项
    getSearchs(){
        const { DispatchReportStore } = this.props;
        const { conditioninfo,form } = DispatchReportStore;
        const {isFormInit} = form;
        let group = [];
        const formParams = form.getFormParams();
        isFormInit && toJS(conditioninfo).map(c =>{
            let items = [];
            c.items.map(fields => {
                if (fields.isQuickSearch) {
                    this.quickSearchPara = fields.domkey[0];
                }
                items.push({
                    com:(<WeaFormItem
                        label={`${fields.label}`}
                        labelCol={{span: `${fields.labelcol}`}}
                        wrapperCol={{span: `${fields.fieldcol}`}}>
                        <WeaSwitch fieldConfig={fields} form={form} formParams={formParams}/>
                    </WeaFormItem>),
                    colSpan:1
                })
            });
            group.push(<WeaSearchGroup needTigger={true} title={c.title} showGroup={c.defaultshow} items={items}/>)
        });
        return group;

    }

    //高级搜索底部按钮
    getTabButtonsAd(){
        const { DispatchReportStore } = this.props;
        const { form, doSearch, setShowSearchAd } = DispatchReportStore;
        return [
            (<Button type="primary" onClick={()=>{
                DispatchReportStore.doSearch({...form.getFormParams()});
                setShowSearchAd(false);
            }}>{getLabel(197, "搜索")}</Button>),
            (<Button type="ghost" onClick={() => form.reset()}>{getLabel(2022, "重置")}</Button>),
            (<Button type="ghost" onClick={() => {
                setShowSearchAd(false);
            }}>{getLabel(201, "取消")}</Button>)
        ]
    }

    //顶部右侧菜单
    getButtons() {
        const { DispatchReportStore} = this.props;
        const { rightMenu,tableStore,form} = DispatchReportStore;
        const count = tableStore.count;
        let isDisabled = count === 0 ;
        let btnArr = [];
        rightMenu.slice().forEach(m => {
            let onClick = () => {
            };
            let content = m.menuName;
            if (m.menuFun.indexOf('doExcel()') >= 0) {
                onClick = () => {
                    DispatchReportStore.exportExcel({...form.getFormParams()});
                }
            }

            btnArr.push(<Button type="primary"
                                disabled={(m.isControl == '1' && isDisabled )}
                                onClick={onClick}>
                {content}
            </Button>);
        });
        return btnArr;
    }
}

//组件检错机制
class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
        return(
            <WeaErrorPage msg={ hasErrorMsg ? this.props.error : getLabel(384697, "对不起，该页面异常，请联系管理员！") } />
        );
    }
}



DispatchReport = WeaTools.tryCatch( React, MyErrorHandler, { error: "" })(DispatchReport);


export default DispatchReport